#!/bin/bash

base_dir=`cd $(dirname $0)/..; pwd`
# 输出目录
output_dir="src/assets"

# 数据文件
data_file="$output_dir/area.js"
# 备份文件
backup_file="$output_dir/area.bak.js"

black=`tput setaf 0`
red=`tput setaf 1`
green=`tput setaf 2`
yellow=`tput setaf 3`
magenta=`tput setaf 5`
white=`tput setaf 7`
bold=`tput bold`
normal=`tput sgr0`

cd ${base_dir}/${output_dir}

printf "$black$(tput setab 7)准备同步省市区数据$normal$bold\n"

# 如果数据文件存在则先做备份
if [ -f "$base_dir/$data_file" ]; then
  echo "开始备份"
  cp $base_dir/$data_file $base_dir/$backup_file
  sleep 1
  echo "备份成功($backup_file)"
else
  echo "没有数据可备份！"
fi

sleep 1
echo "下载数据"
curl --progress-bar -o $base_dir/$data_file https://raw.githubusercontent.com/youzan/vant/dev/src/area/demo/area.js
if [ $? -ne 0 ]; then
  echo "下载失败，正在恢复备份数据.."
  mv $base_dir/$backup_file $base_dir/$data_file
  sleep 1s
  echo "备份数据恢复完毕！"
else
  echo "\033[0;92m同步完成！\033[0;37m"
fi
